#include "WidgetHandler.h"
#include "LogoUtility.h"
#include "McuWorker.h"

WidgetHandler::WidgetHandler()
{

}

WidgetHandler* WidgetHandler::getInstance()
{
    static WidgetHandler singleton{};
    return &singleton;
}

void WidgetHandler::init(QWidget* parent)
{
    g_parent = parent;
    addWidgets();
    bindingSignal();
    LogoUtility::getInstance()->loadLogo();
}

void WidgetHandler::addWidgets()
{
    WidgetDescription* WidgetDesc;

    /* BootUi */
    WidgetDesc = new WidgetDescription();
    g_bootUi = new BootUi(g_parent);
    WidgetDesc->widget = g_bootUi;
    WidgetDesc->x = 0;
    WidgetDesc->y = 0;
    this->g_allWidgets.insert(BootUi_Id, WidgetDesc);

    /* LoginUi */
    WidgetDesc = new WidgetDescription();
    g_loginUi = new LoginUi(g_parent);
    WidgetDesc->widget = g_loginUi;
    WidgetDesc->x = 0;
    WidgetDesc->y = 0;
    this->g_allWidgets.insert(LoginUi_Id, WidgetDesc);

    /* HomeUi */
    WidgetDesc = new WidgetDescription();
    g_homeUi = new HomeUi(g_parent);
    WidgetDesc->widget = g_homeUi;
    WidgetDesc->x = 0;
    WidgetDesc->y = 0;
    this->g_allWidgets.insert(HomeUi_Id, WidgetDesc);

    /* ImageSettingUi */
    WidgetDesc = new WidgetDescription();
    g_imageSettingUi = new ImageSettingUi(g_parent);
    WidgetDesc->widget = g_imageSettingUi;
    WidgetDesc->x = 0;
    WidgetDesc->y = 0;
    this->g_allWidgets.insert(ImageSettingUi_Id, WidgetDesc);

    /* SystemSettingUi */
    WidgetDesc = new WidgetDescription();
    g_systemSettingUi = new SystemSettingUi(g_parent);
    WidgetDesc->widget = g_systemSettingUi;
    WidgetDesc->x = 0;
    WidgetDesc->y = 0;
    this->g_allWidgets.insert(SystemSettingUi_Id, WidgetDesc);

    /* AddPatientUi */
    WidgetDesc = new WidgetDescription();
    g_addPatientUi = new AddPatientUi(g_parent);
    WidgetDesc->widget = g_addPatientUi;
    WidgetDesc->x = 0;
    WidgetDesc->y = 0;
    this->g_allWidgets.insert(AddPatientUi_Id, WidgetDesc);

    /* QueryPatientUi */
    WidgetDesc = new WidgetDescription();
    g_queryPatientUi = new QueryPatientUi(g_parent);
    WidgetDesc->widget = g_queryPatientUi;
    WidgetDesc->x = 0;
    WidgetDesc->y = 0;
    this->g_allWidgets.insert(QueryPatientUi_Id, WidgetDesc);

    /* PatientInfoUi */
    WidgetDesc = new WidgetDescription();
    g_patientInfoUi = new PatientInfoUi(g_parent);
    WidgetDesc->widget = g_patientInfoUi;
    WidgetDesc->x = 0;
    WidgetDesc->y = 0;
    this->g_allWidgets.insert(PatientInfoUi_Id, WidgetDesc);

    /* ModifyPatientUi */
    WidgetDesc = new WidgetDescription();
    g_modifyPatientUi = new ModifyPatientUi(g_parent);
    WidgetDesc->widget = g_modifyPatientUi;
    WidgetDesc->x = 0;
    WidgetDesc->y = 0;
    this->g_allWidgets.insert(ModifyPatientUi_Id, WidgetDesc);

    /* PhotoPlayerUi */
    WidgetDesc = new WidgetDescription();
    g_photoPlayerUi = new PhotoPlayerUi(g_parent);
    WidgetDesc->widget = g_photoPlayerUi;
    WidgetDesc->x = 0;
    WidgetDesc->y = 0;
    this->g_allWidgets.insert(PhotoPlayerUi_Id, WidgetDesc);

    /* VideoPlayerUi */
    WidgetDesc = new WidgetDescription();
    g_videoPlayerUi = new VideoPlayerUi(g_parent);
    WidgetDesc->widget = g_videoPlayerUi;
    WidgetDesc->x = 0;
    WidgetDesc->y = 0;
    this->g_allWidgets.insert(VideoPlayerUi_Id, WidgetDesc);

    /* EngineerUi */
    WidgetDesc = new WidgetDescription();
    g_engineerUi = new EngineerUi(g_parent);
    WidgetDesc->widget = g_engineerUi;
    WidgetDesc->x = 0;
    WidgetDesc->y = 0;
    this->g_allWidgets.insert(EngineerUi_Id, WidgetDesc);

    foreach (WidgetDescription* widgetDesc, this->g_allWidgets.values()) {
        widgetDesc->widget->hide();
    }
}

void WidgetHandler::bindingSignal()
{
    QObject::connect(McuWorker::getInstance(), &McuWorker::updateMirrorTypeSignal,
                     g_imageSettingUi, &ImageSettingUi::changeMirrorTypeSlot);

    QObject::connect(McuWorker::getInstance(), &McuWorker::updateImageSettingItemUiSignal,
                     g_imageSettingUi, &ImageSettingUi::updateImageSettingItemUiSlot);

    QObject::connect(McuWorker::getInstance(), &McuWorker::recordSignal,
                     g_homeUi, &HomeUi::recordSlot);

    QObject::connect(McuWorker::getInstance(), &McuWorker::takePhotoSignal,
                     g_homeUi, &HomeUi::takePhotoSlot);

    QObject::connect(McuWorker::getInstance(), &McuWorker::updateToastSignal,
                     g_homeUi, &HomeUi::updateToastSlot);
}

void WidgetHandler::displayWidget(WidgetId id)
{
    int x = 0;
    int y = 0;

    WidgetDescription* display = this->g_allWidgets.value(id, nullptr);
    if(nullptr == display) {
        qInfo()<<"display widget is null, id = "<<id;
        return;
    }

    if (g_currentWidgetId != Null_Id) {
        /* 隐藏当前widget */
        hideWidget(g_currentWidgetId);
    }
    display->widget->show();
    x = display->x;
    y = display->y;
    display->widget->move(x, y);
    display->widget->activate();
    g_currentWidgetId = id;
}

void WidgetHandler::hideWidget(WidgetId id)
{
    WidgetDescription* hide = this->g_allWidgets.value(id, nullptr);
    if(nullptr == hide) {
        qInfo()<<"hide widget is null, id = "<<id;
        return;
    }
    hide->widget->hide();
    hide->widget->inactivate();
}

BaseWidget* WidgetHandler::getWidget(WidgetId id)
{
    WidgetDescription* win = this->g_allWidgets.value(id, nullptr);
    if(nullptr == win) {
        qInfo()<<"widget is null, id = "<<id;
        return nullptr;
    }

    return win->widget;
}
